package com.xxx.shetuan3.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxx.shetuan3.dao.OrganizationDao;
import com.xxx.shetuan3.exception.ApiException;
import com.xxx.shetuan3.pojo.Organization;
import com.xxx.shetuan3.pojo.StudentAndOrganization;
import com.xxx.shetuan3.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrganizationServiceImpl implements OrganizationService {
    @Autowired
    private OrganizationDao organizationDao;
    @Override
    public boolean saveOrganization(Organization organization) {
        return organizationDao.saveOrganization(organization);
    }

    @Override
    public Organization findOrganizationById(Integer organizationId) {
        return organizationDao.findOrganizationById(organizationId);
    }


    @Override
    public boolean updateOrganization(Organization organization) {
        return organizationDao.updateOrganization(organization);
    }

    @Override
    public boolean deleteOrganizationById(Integer organizationId) {
        return organizationDao.deleteOrganizationById(organizationId);
    }

    @Override
    public boolean stopOrganization(Integer organizationId) {
        Organization organization = organizationDao.findOrganizationById(organizationId);
        if(organization==null){
            throw new ApiException("社团不存在");
        }else{
            organization.setEnd(new Date(System.currentTimeMillis()));
            organizationDao.updateOrganization(organization);
        }
        return true;
    }

    @Override
    public boolean restartOrganization(Integer organizationId) {
        Organization organization = organizationDao.findOrganizationById(organizationId);
        if(organization==null){
            throw new ApiException("社团不存在");
        }else{
            organization.setEnd(null);
            organizationDao.updateOrganization(organization);
        }
        return true;
    }

    @Override
    public List<Organization> findAll() {
        return organizationDao.findAll();
    }

    @Override
    public List<Organization> queryPage(Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Organization> organizationList=organizationDao.findAll();
        PageInfo<Organization> pageInfo = new PageInfo<>(organizationList);
        return pageInfo.getList();
    }
    @Override
    public PageInfo queryPage1(Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Organization> organizationList=organizationDao.findAll();
        PageInfo<Organization> pageInfo = new PageInfo<>(organizationList);
        return pageInfo;
    }
    @Override
    public List<Organization> findPageByChiefId(Integer chiefId, Integer start, Integer size) {
        PageHelper.startPage(start,size);
        List<Organization> organizationList=organizationDao.findByChiefId(chiefId);
        PageInfo<Organization> pageInfo = new PageInfo<>(organizationList);
        return pageInfo.getList();
    }

}
